package com.ww.util.guava;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Splitter;

public class StrToCollection {

	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		String a = "1-2-3-4-5-6";
		String[] strs = a.split("-");
		for (int i = 0; i < strs.length; i++) {
			list.add(strs[i]);
		}
		/**
		 * 打印结果：["1","2","3","4","5","6"]
		 */
		System.out.println(JSON.toJSONString(list));
		
		System.out.println("*********************");
		
		/**
		 * 将String转换为特定的集合		
		 */
		List<String> list2 = Splitter.on("-").splitToList(a);
		/**
		 * 打印结果：["1","2","3","4","5","6"]
		 */
		System.out.println(JSON.toJSONString(list2));

		/**
		 * guava使用omitEmptyStrings().trimResults()去除空串与空格
		 */
		String str2 = "1-2-3-4-  5-  6   ";
		List<String> list3 = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(str2);
		System.out.println(JSON.toJSONString(list3));

		/**
		 * String转换为map
		 */
		String str3 = "xiaoming=11,xiaohong=22";
		Map<String, String> map = Splitter.on(",").withKeyValueSeparator("=").split(str3);
		/**
		 * 打印结果：
		 * {"xiaoming":"11","xiaohong":"22"}
		 */
		System.out.println(JSON.toJSONString(map));
	}
}
